#include "StdAfx.h"
#include "Resample.h"
#include <cmath>

CResample::CResample(const double & dResampleRate)
	: m_dResampleRate(dResampleRate)
{
	ASSERT(dResampleRate >= 1.0);
	m_nA = int(dResampleRate / 2) + 1;
}

CResample::~CResample(void)
{
}

void CResample::Init()
{
	m_nPos = 0;
	m_dPos = m_dResampleRate / 2;
}

double h(double x, int a)
{
	if (x == 0.0)
		return 1.0;
	if (x > a || x < -a)
		return 0;

	return a * sin(x) * sin(x/a) / (x*x);
}

BOOL CResample::Add(const CCachedQueue<CFrame> & queue, SHORT & nResult)
{
	m_nPos++;

	if (m_nPos < m_dPos)
		return FALSE;

	double dResult = 0;

	for (SSIZE_T i = -m_nA; i < m_nA+1; i++)
		dResult += queue[i].chData * h(m_nPos+i-m_dPos, m_nA);

	nResult = (SHORT) dResult;



	m_dPos += m_dResampleRate;
	return TRUE;
}